<?
	session_start();
	require_once "librarys/funs/classdb.php";
   	$d = new db;

	if ( !isset($_SESSION['dayidSP']) ) $_SESSION['dayidSP']=array();
	if ( !isset($_SESSION['daySoLuong']) ) $_SESSION['daySoLuong']=array();
	if ( !isset($_SESSION['dayDonGia']) )  $_SESSION['dayDonGia']=array();
	if ( !isset($_SESSION['dayTenSP']) )   $_SESSION['dayTenSP']=array();
	if ( !isset($_SESSION['dayMoTa']) )    $_SESSION['dayMoTa']=array();
	if ( !isset($_SESSION['dayUrlHinh']) ) $_SESSION['dayUrlHinh']=array();
	if ( !isset($_SESSION['dayGiamGia']) ) $_SESSION['dayGiamGia']=array();

	$idSP = $_GET["idSP"];
	$action = $_GET['action'];
	$listid=$_GET['listid'];
	$soluong =(isset($_GET['soluong'])==true)? $_GET['soluong']:1;
	//tiep nhan va kiem tra du lieu..
	settype($idSP,"int");
	settype($soluong,"int");
	$act=array("updatevi", "updateen","addvi","adden","remove","removeAll","updateorderlangvi","updateorderlangen");
	if (in_array($action , $act)==false) exit();

	if ($action=="removeAll"){
		unset($_SESSION['dayTenSP']);
		unset($_SESSION['dayDonGia']);
		unset($_SESSION['daySoLuong']);
		unset($_SESSION['dayMoTa']);
		unset($_SESSION['dayUrlHinh']);
		echo "<p align =center>Giỏ hàng không còn sản phẩm.</p>";
		exit();
	}

	//if ($idSP<=0) die(" Không biết sản phẩm nào để thêm/bớt");
	if ($action=="remove") {
		unset($_SESSION['dayidSP'][$idSP]);
		unset($_SESSION['dayTenSP'][$idSP]);
		unset($_SESSION['dayDonGia'][$idSP]);
		unset($_SESSION['daySoLuong'][$idSP]);
		unset($_SESSION['dayMoTa'][$idSP]);
		unset($_SESSION['dayUrlHinh'][$idSP]);
	}

	if ($action=="addvi") {
		$sql ="SELECT * FROM sanpham WHERE id=$idSP";
		$sanpham = mysql_query($sql) or die (mysql_error());
                //echo $sql;die;
		$row_sanpham = mysql_fetch_assoc($sanpham);

		$_SESSION['dayidSP'][$idSP] = $row_sanpham['id'];
		$_SESSION['dayTenSP'][$idSP] = $row_sanpham['ten_vi'];
		$_SESSION['dayDonGia'][$idSP] = $row_sanpham['gia'];
		$_SESSION['daySoLuong'][$idSP] += $soluong;
		$_SESSION['dayUrlHinh'][$idSP] = $d->folderHinh.$row_sanpham['url_img'];
	}

	if ($action=="adden") {
		$sql ="SELECT * FROM sanpham WHERE idsp=$idSP";
		$sanpham = mysql_query($sql) or die (mysql_error());
		$row_sanpham = mysql_fetch_assoc($sanpham);

		$_SESSION['dayidSP'][$idSP] = $row_sanpham['id'];
		$_SESSION['dayTenSP'][$idSP] = $row_sanpham['ten_en'];
		$_SESSION['dayDonGia'][$idSP] = $row_sanpham['gia'];
		$_SESSION['daySoLuong'][$idSP] += $soluong;
		$_SESSION['dayUrlHinh'][$idSP] = $d->folderHinh.$row_sanpham['url_img'];
	}

	if ($action=="updatevi") {
		$sql="SELECT * FROM sanpham WHERE id=$idSP";
		$sanpham=mysql_query($sql) or die (mysql_error());
		$row_sanpham=mysql_fetch_assoc($sanpham);

		$_SESSION['dayidSP'][$idSP] = $row_sanpham['idsp'];
		$_SESSION['dayidSP'][$idSP] = $row_sanpham['id'];
		$_SESSION['dayTenSP'][$idSP] = $row_sanpham['ten_vi'];
		$_SESSION['dayDonGia'][$idSP] = $row_sanpham['gia'];
		$_SESSION['daySoLuong'][$idSP] = $soluong;
		$_SESSION['dayUrlHinh'][$idSP] = $d->folderHinh.$row_sanpham['url_img'];
	}

	if ($action=="updateen") {
		$sql="SELECT idsp,tensp_en,gia_en,urlhinh FROM sanpham WHERE idsp=$idSP";
		$sanpham=mysql_query($sql) or die (mysql_error());
		$row_sanpham=mysql_fetch_assoc($sanpham);

		$_SESSION['dayidSP'][$idSP] = $row_sanpham['id'];
		$_SESSION['dayTenSP'][$idSP] = $row_sanpham['ten_vi'];
		$_SESSION['dayDonGia'][$idSP] = $row_sanpham['gia'];
		$_SESSION['daySoLuong'][$idSP] = $soluong;
		$_SESSION['dayUrlHinh'][$idSP] = $d->folderHinh.$row_sanpham['url_img'];
	}

	if ($action=="updateorderlangvi") {
		$soid = count( $_SESSION['dayidSP'] );
		if ($soid<=0) return 0;
		reset( $_SESSION['dayidSP'] );

		for ($i = 0; $i<$soid ; $i++) {
		$idSP = current( $_SESSION['dayidSP'] );
		$sql="SELECT idsp,tensp_vi,gia_vi,urlhinh FROM sanpham WHERE idsp=$idSP";
		$sanpham=mysql_query($sql) or die (mysql_error());
		$row_sanpham=mysql_fetch_assoc($sanpham);

		$_SESSION['dayidSP'][$idSP] = $row_sanpham['idsp'];
		$_SESSION['dayTenSP'][$idSP] = $row_sanpham['tensp_vi'];
		$_SESSION['dayDonGia'][$idSP] = $row_sanpham['gia_vi'];

		next( $_SESSION['dayidSP'] );
		}

	}
	if ($action=="updateorderlangen") {
		$soid = count( $_SESSION['dayidSP'] );
		if ($soid<=0) return 0;
		reset( $_SESSION['dayidSP'] );

		for ($i = 0; $i<$soid ; $i++) {
		$idSP = current( $_SESSION['dayidSP'] );
		$sql="SELECT idsp,tensp_en,gia_en,urlhinh FROM sanpham WHERE idsp=$idSP";
		$sanpham=mysql_query($sql) or die (mysql_error());
		$row_sanpham=mysql_fetch_assoc($sanpham);

		$_SESSION['dayidSP'][$idSP] = $row_sanpham['idsp'];
		$_SESSION['dayTenSP'][$idSP] = $row_sanpham['tensp_en'];
		$_SESSION['dayDonGia'][$idSP] = $row_sanpham['tensp_en'];

		next( $_SESSION['dayidSP'] );
		}

	}
?>